#include "axissettingview.h"
#include "ui_axissettingview.h"
#include <qmessagebox.h>
#include "../CommunicateDelegate/communicatenormaldelegate.h"
#include <QScrollArea>
#include "instructionEnum.h"

AxisSettingView::AxisSettingView(int styleId,CommunicateNormalDelegate *communicateNormalIn, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AxisSettingView)
{
    communicateNormal=communicateNormalIn;
    isInitialOk=false;
    move(0,0);
    ui->setupUi(this);
    initialDisplayStyle(styleId);

    for(int i=0;i<D_ROBOT_AXIS_MAX;i++)
    {
        QString tmpStr;
        tmpStr="J";
        tmpStr+=QString::number(i+1);
        ui->comboBox_axisIndex->insertItem(i,tmpStr);
    }
    isInitialOk=true;

}

void AxisSettingView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,620,570);
        ui->scrollArea->setGeometry(10,40,600,460);

        ui->pushButton_return->setGeometry(620-100,570-40,90,30);
        break;
    default:
        break;
    }
}

AxisSettingView::~AxisSettingView()
{
    delete ui;
}

void AxisSettingView::showView_slot()
{
//    if(communicateNormal)
//    {
        AxisProperty_hmi axisPropertyOut;
        communicateNormal->getAxisProperty(ui->comboBox_axisIndex->currentIndex(),axisPropertyOut);
        showConfigInfo(axisPropertyOut);
        updateShowSafetyCheck();
        updateCoordinateConfig();
//    }

    show();
}

void AxisSettingView::on_pushButton_changeConfig_clicked()
{
    if(false==isAuthorityOk())
    {
        return ;
    }
    int returnFlag;
    AxisProperty_hmi axisPropertyIn;
    int axisId=ui->comboBox_axisIndex->currentIndex();
    axisPropertyIn.minPosition=ui->lineEdit_minPosition->text().toDouble();
    axisPropertyIn.maxPosition=ui->lineEdit_maxPosition->text().toDouble();
    axisPropertyIn.approachDistance=ui->lineEdit_approachDistance->text().toDouble();
    axisPropertyIn.maxVelocity=ui->lineEdit_maxVelCommand->text().toDouble();
    axisPropertyIn.maxFeedbackVelocity=ui->lineEdit_maxVelFeedback->text().toDouble();
    axisPropertyIn.maxAccelation=ui->lineEdit_maxAcc->text().toDouble();
    axisPropertyIn.processVelocityFollowError=ui->lineEdit_processVelError->text().toDouble();
    axisPropertyIn.processFollowError=ui->lineEdit_processPositionError->text().toDouble();
    axisPropertyIn.targetFollowError=ui->lineEdit_targetError->text().toDouble();
    axisPropertyIn.jogTorqueLimit=ui->lineEdit_jogTorque->text().toDouble();
    axisPropertyIn.motorRatedTorque=ui->lineEdit_motorRatedTorque->text().toDouble();
    axisPropertyIn.motorTorqueLimitPositive=ui->lineEdit_motorPositiveTorque->text().toDouble();
    axisPropertyIn.motorTorqueLimitNegative=ui->lineEdit_motorNegativeTorque->text().toDouble();
    axisPropertyIn.motorRatedTorque=ui->lineEdit_motorRatedTorque->text().toDouble();
    axisPropertyIn.maxTorque=ui->lineEdit_maxTorque->text().toDouble();


    communicateNormal->setAxisProperty(axisId,axisPropertyIn,returnFlag);
    if(1==returnFlag)
    {
        QMessageBox::information(NULL, "提示", "参数修改成功,立刻生效！", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "提示", "设置失败！", QMessageBox::Yes );
    }
}

bool AxisSettingView::isAuthorityOk()
{
    QString code=ui->lineEdit_vehicle_calibrate_authority_code->text();
    if(code=="123456")
    {
        ui->lineEdit_vehicle_calibrate_authority_code->setText("");
        return true;
    }
    else
    {
        QMessageBox::information(NULL, "information", "授权码不对，请重新输入", QMessageBox::Yes );
//        ui->lineEdit_vehicle_calibrate_authority_code->setText("请重新输入授权码");
        return false;
    }
    return false;
}

void AxisSettingView::showConfigInfo(AxisProperty_hmi axisPropertyIn)
{
    ui->lineEdit_minPosition->setText(QString::number(axisPropertyIn.minPosition));
    ui->lineEdit_maxPosition->setText(QString::number(axisPropertyIn.maxPosition));
    ui->lineEdit_approachDistance->setText(QString::number(axisPropertyIn.approachDistance));
    ui->lineEdit_maxVelCommand->setText(QString::number(axisPropertyIn.maxVelocity));
    ui->lineEdit_maxVelFeedback->setText(QString::number(axisPropertyIn.maxFeedbackVelocity));
    ui->lineEdit_maxAcc->setText(QString::number(axisPropertyIn.maxAccelation));
    ui->lineEdit_processVelError->setText(QString::number(axisPropertyIn.processVelocityFollowError));
    ui->lineEdit_processPositionError->setText(QString::number(axisPropertyIn.processFollowError));
    ui->lineEdit_targetError->setText(QString::number(axisPropertyIn.targetFollowError));
    ui->lineEdit_jogTorque->setText(QString::number(axisPropertyIn.jogTorqueLimit));
    ui->lineEdit_motorRatedTorque->setText(QString::number(axisPropertyIn.motorRatedTorque));
    ui->lineEdit_motorPositiveTorque->setText(QString::number(axisPropertyIn.motorTorqueLimitPositive));
    ui->lineEdit_motorNegativeTorque->setText(QString::number(axisPropertyIn.motorTorqueLimitNegative));
    ui->lineEdit_motorRatedTorque->setText(QString::number(axisPropertyIn.motorRatedTorque));
    ui->lineEdit_maxTorque->setText(QString::number(axisPropertyIn.maxTorque));

}

void AxisSettingView::changeSafetyCheck(E_SAFETY_CHECK typeIn, int isEnable)
{
    if(false==ui->checkBox_enableChange->isChecked())
    {
        updateShowSafetyCheck();
        return;
    }
    if(false==isAuthorityOk())
    {
        updateShowSafetyCheck();
        return ;
    }
    int returnFlag;

    communicateNormal->setSefetyCheck(typeIn,isEnable,returnFlag);
    if(1==returnFlag)
    {
        QMessageBox::information(NULL, "提示", "安全参数修改成功,立刻生效！", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "提示", "设置失败！", QMessageBox::Yes );
    }
}

void AxisSettingView::updateShowSafetyCheck()
{

    int checkStatusReturn;
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_emergencyStopMonitorFlag,checkStatusReturn);
    ui->comboBox_emmergencyDi->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_hmiServoDiMonitorFlag,checkStatusReturn);
    ui->comboBox_hmiServoOn->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_powerCheckFlag,checkStatusReturn);
    ui->comboBox_powerCheck->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_slowDownFlag1,checkStatusReturn);
    ui->comboBox_slowdown1->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_slowDownFlag2,checkStatusReturn);
    ui->comboBox_slowdown2->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_slowDownFlag3,checkStatusReturn);
    ui->comboBox_slowdown3->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_slowDownFlag4,checkStatusReturn);
    ui->comboBox_slowdown4->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_enableRun1Flag,checkStatusReturn);
    ui->comboBox_enablerun1->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_enableRun2Flag,checkStatusReturn);
    ui->comboBox_enablerun2->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_enableRun3Flag,checkStatusReturn);
    ui->comboBox_enablerun3->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_enableRun4Flag,checkStatusReturn);
    ui->comboBox_enablerun4->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_resetAlarmFlag,checkStatusReturn);
    ui->comboBox_resetalarm->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_extServOnFlag,checkStatusReturn);
    ui->comboBox_extServoon->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_stopFlag,checkStatusReturn);
    ui->comboBox_stopdi->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_ceaseFlag,checkStatusReturn);
    ui->comboBox_ceasedi->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_runFlag,checkStatusReturn);
    ui->comboBox_rundi->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_autoStartUpServOnFlag,checkStatusReturn);
    ui->comboBox_autoServoon->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_runSafetyCheck1Flag,checkStatusReturn);
    ui->comboBox_runcheck1->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_runSafetyCheck2Flag,checkStatusReturn);
    ui->comboBox_runcheck2->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_runSafetyCheck3Flag,checkStatusReturn);
    ui->comboBox_runcheck3->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_runSafetyCheck4Flag,checkStatusReturn);
    ui->comboBox_runcheck4->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_ultrasonicCheckFlag,checkStatusReturn);
    ui->comboBox_ultrasonic->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_isEnableRun_autoMode,checkStatusReturn);
    ui->comboBox_enablerunautomode->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_isPlayErrorMusic,checkStatusReturn);
    ui->comboBox_errormusic->setCurrentIndex(checkStatusReturn);
    communicateNormal->getSefetyCheck(E_SAFETY_CHECK_isPlayBarrierMusic,checkStatusReturn);
    ui->comboBox_barriermusic->setCurrentIndex(checkStatusReturn);


}

void AxisSettingView::updateCoordinateConfig()
{
    struct RobotCoordinateAxisConfig configOut;
    communicateNormal->getRobotCoordinateConfig(configOut);
    ui->lineEdit_lineMaxVelocity->setText(QString::number(configOut.lineMaxSpeed));
    ui->lineEdit_lineMaxAcceleration->setText(QString::number(configOut.lineMaxAcceleration));
    ui->lineEdit_rotate_equirdius->setText(QString::number(configOut.rotate_eqradius));
    ui->lineEdit_ceaseTimeRatio->setText(QString::number(configOut.arm_CeaseTime));
    ui->lineEdit_stopTimeRatio->setText(QString::number(configOut.arm_StopTime));
    ui->lineEdit_t1Ratio->setText(QString::number(configOut.t1RatioLimit));
    ui->lineEdit_targetWaitTime->setText(QString::number(configOut.targetWaitTime));
    ui->lineEdit_jogDecartLineRatio->setText(QString::number(configOut.jogDecartLineRatio));
    ui->lineEdit_jogDecartRotateRatio->setText(QString::number(configOut.jogDecartRotateRation));


}


void AxisSettingView::on_pushButton_getConfig_clicked()
{
    AxisProperty_hmi axisPropertyOut;
    communicateNormal->getAxisProperty(ui->comboBox_axisIndex->currentIndex(),axisPropertyOut);
    showConfigInfo(axisPropertyOut);
}

void AxisSettingView::on_comboBox_axisIndex_currentIndexChanged(int index)
{
    if(false==isInitialOk)
    {
        return ;
    }
    AxisProperty_hmi axisPropertyOut;
    communicateNormal->getAxisProperty(index,axisPropertyOut);
    showConfigInfo(axisPropertyOut);
    qDebug()<<"获取轴参数ok axis"<<index;
}

void AxisSettingView::on_pushButton_return_clicked()
{
    hide();
}

void AxisSettingView::on_comboBox_emmergencyDi_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_emergencyStopMonitorFlag,index);
}

void AxisSettingView::on_comboBox_hmiServoOn_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_hmiServoDiMonitorFlag,index);
}

void AxisSettingView::on_comboBox_powerCheck_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_powerCheckFlag,index);
}

void AxisSettingView::on_comboBox_slowdown1_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_slowDownFlag1,index);
}

void AxisSettingView::on_comboBox_slowdown2_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_slowDownFlag2,index);
}

void AxisSettingView::on_comboBox_slowdown3_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_slowDownFlag3,index);
}

void AxisSettingView::on_comboBox_slowdown4_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_slowDownFlag4,index);
}

void AxisSettingView::on_comboBox_enablerun1_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_enableRun1Flag,index);
}

void AxisSettingView::on_comboBox_enablerun2_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_enableRun2Flag,index);
}

void AxisSettingView::on_comboBox_enablerun3_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_enableRun3Flag,index);
}

void AxisSettingView::on_comboBox_enablerun4_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_enableRun4Flag,index);
}

void AxisSettingView::on_comboBox_resetalarm_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_resetAlarmFlag,index);
}

void AxisSettingView::on_comboBox_extServoon_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_extServOnFlag,index);
}

void AxisSettingView::on_comboBox_rundi_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_runFlag,index);
}

void AxisSettingView::on_comboBox_ceasedi_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_ceaseFlag,index);
}

void AxisSettingView::on_comboBox_stopdi_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_stopFlag,index);
}

void AxisSettingView::on_comboBox_autoServoon_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_autoStartUpServOnFlag,index);
}

void AxisSettingView::on_comboBox_runcheck1_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_runSafetyCheck1Flag,index);
}

void AxisSettingView::on_comboBox_runcheck2_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_runSafetyCheck2Flag,index);
}

void AxisSettingView::on_comboBox_runcheck3_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_runSafetyCheck3Flag,index);
}

void AxisSettingView::on_comboBox_runcheck4_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_runSafetyCheck4Flag,index);
}

void AxisSettingView::on_comboBox_ultrasonic_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_ultrasonicCheckFlag,index);
}

void AxisSettingView::on_comboBox_enablerunautomode_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_isEnableRun_autoMode,index);
}

void AxisSettingView::on_comboBox_errormusic_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_isPlayErrorMusic,index);
}

void AxisSettingView::on_comboBox_barriermusic_currentIndexChanged(int index)
{
    changeSafetyCheck(E_SAFETY_CHECK_isPlayBarrierMusic,index);
}

void AxisSettingView::on_pushButton_getSafeConfig_clicked()
{
    updateShowSafetyCheck();
}

void AxisSettingView::on_pushButton_getRobotCoordinateParameter_clicked()
{
    updateCoordinateConfig();
}

void AxisSettingView::on_pushButton_setRobotCoordinateParameter_clicked()
{

    if(false==isAuthorityOk())
    {
        updateShowSafetyCheck();
        return ;
    }
    int returnFlag;

    RobotCoordinateAxisConfig configIn;
    configIn.lineMaxSpeed=ui->lineEdit_lineMaxVelocity->text().toDouble();
    configIn.lineMaxAcceleration=ui->lineEdit_lineMaxAcceleration->text().toDouble();
    configIn.rotate_eqradius=ui->lineEdit_rotate_equirdius->text().toDouble();
    configIn.arm_CeaseTime=ui->lineEdit_ceaseTimeRatio->text().toDouble();
    configIn.arm_StopTime=ui->lineEdit_stopTimeRatio->text().toDouble();
    configIn.t1RatioLimit=ui->lineEdit_t1Ratio->text().toDouble();
    configIn.targetWaitTime=ui->lineEdit_targetWaitTime->text().toDouble();
    configIn.jogDecartLineRatio=ui->lineEdit_jogDecartLineRatio->text().toDouble();
    configIn.jogDecartRotateRation=ui->lineEdit_jogDecartRotateRatio->text().toDouble();

    communicateNormal->setRobotCoordinateConfig(configIn,returnFlag);
    if(1==returnFlag)
    {
        QMessageBox::information(NULL, "提示", "笛卡尔参数修改成功,立刻生效！", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "提示", "设置失败！", QMessageBox::Yes );
    }
}
